package com.lizx.framework.web.smart.framework.core.helper;

import com.lizx.framework.web.smart.framework.core.annotations.SmartController;
import com.lizx.framework.web.smart.framework.core.annotations.SmartService;
import com.lizx.framework.web.smart.framework.core.utils.SmartClassLoadUtil;

import java.util.HashSet;
import java.util.Set;

/**
 * 类操作工具
 *
 * @Author: lizx
 * @Description:
 * @Date: Create in 2019/1/17 4:21 PM
 */
public class SmartClassHelper {

    /**
     * 自定义加载器加载的类集合
     */
    private static final Set<Class<?>> CLASS_SET;

    static {
        String basePackage = SmartConfigHelper.getAppBasePackage();
        CLASS_SET = SmartClassLoadUtil.getClassSet(basePackage);
    }

    /**
     * 获取应用包下的所有类
     *
     * @return
     */
    public static Set<Class<?>> getClassSet() {
        return CLASS_SET;
    }

    /**
     * 获取应用包下的所有service
     *
     * @return
     */
    public static Set<Class<?>> getServiceClassSet() {
        Set<Class<?>> classSet = new HashSet<>();
        for (Class<?> cls : CLASS_SET) {
            if (cls.isAnnotationPresent(SmartService.class)) {
                classSet.add(cls);
            }
        }
        return classSet;
    }

    /**
     * 获取应用包下的所有controller
     *
     * @return
     */
    public static Set<Class<?>> getControllerClassSet() {
        Set<Class<?>> classSet = new HashSet<>();
        for (Class<?> cls : CLASS_SET) {
            if (cls.isAnnotationPresent(SmartController.class)) {
                classSet.add(cls);
            }
        }
        return classSet;
    }

    /**
     * 获取应用包下的所有bean
     *
     * @return
     */
    public static Set<Class<?>> getBeanClassSet() {
        Set<Class<?>> beanClassSet = new HashSet<>();
        beanClassSet.addAll(getServiceClassSet());
        beanClassSet.addAll(getControllerClassSet());
        return beanClassSet;
    }

}
